import { Kysely } from 'kysely';
import { D1Dialect } from 'kysely-d1';
import { User, Session, Account, Verification } from '~/db/schema/auth';
import { Driver } from '~/db/schema/drivers';
import { Feed } from '~/db/schema/feeds';
import { Link } from '~/db/schema/links';

export type Dababase = {
  users: User;
  sessions: Session;
  accounts: Account;
  verifications: Verification;
  drivers: Driver;
  feeds: Feed;
  links: Link;
}

let db: Kysely<Dababase>;

export const initDb = (env: Env) => {
  if (!db) {
    db = new Kysely<Dababase>({
      dialect: new D1Dialect({ database: env.D1DATA }),
    });
  }
  return db;
};
